package com.xtravel.common.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.xtravel.R;
import com.xtravel.application.XtravelApplication;
import com.xtravel.common.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.AuthenticationException;

import java.io.IOException;

public class BaseActivity extends FragmentActivity {

    protected static final String EX_KEY = "ex";

    /**
     * 消息按钮.
     */
    protected LinearLayout imgMsgll;

    protected RelativeLayout rlMsg;

    public BaseActivity() {
    }

    public void onBack(View view) {
        this.onBackPressed();
    }

    /**
     * 设置header标题.
     *
     * @param title 标题.
     */
    protected void setHeaderTitle(String title) {
        TextView tvTitle = (TextView) findViewById(R.id.header_title);
        if (StringUtils.isBlank(title)) {
            tvTitle.setText(R.string.app_name);
        } else {
            tvTitle.setText(title);
        }
    }

    /**
     * 设置返回按钮可用与否.
     *
     * @param visibility 显示与否.
     */
    protected void setBackBtVisiable(int visibility) {
        Button btBack = (Button) findViewById(R.id.header_back);
        btBack.setVisibility(visibility);
    }

    /**
     * 设置消息返回按钮可用与否.
     *
     * @param visibility 显示与否.
     */
    protected void setMsgImgBtnVisible(int visibility) {
        if (imgMsgll == null) {
            imgMsgll = (LinearLayout)findViewById(R.id.header_msg);
        }
        if (imgMsgll != null) {
            imgMsgll.setVisibility(visibility);
        }
        if (rlMsg == null) {
            rlMsg = (RelativeLayout) findViewById(R.id.header_msg_rl);
        }
        if (rlMsg != null) {
            rlMsg.setVisibility(visibility);
        }
    }

    protected Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle bundle = msg.getData();
            if (bundle != null && bundle.getSerializable(EX_KEY) != null) {
                Exception e = (Exception) bundle.getSerializable(EX_KEY);
                if (e instanceof BusinessException) {
                    // 业务异常
                    XtravelApplication.showToastShort(e.getMessage());
                } else if (e instanceof AuthenticationException) {
                    // 未登录异常
                    XtravelApplication.showToastShort("登录后再来吧");
                    /*Intent intent = new Intent(BaseActivity.this, UserSigninActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    BaseActivity.this.startActivity(intent);*/
                } else if (e instanceof IOException) {
                    // 网络异常
                    XtravelApplication.showToastShort("网络似乎不能用了");
                } else {
                    e.printStackTrace();
                }
            }
        }
    };

    protected void handleException(Exception e) {
        Message msg = handler.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putSerializable(EX_KEY, e);
        msg.setData(bundle);
        handler.sendMessage(msg);
    }

    protected void handleExceptionQuiet(Exception e) {
    }

}
